'use strict';

define(['controllers', '../service/statService', './filterController', 'highcharts'], function (controllers) {
    controllers.stat.register('VisitController', ['$scope', '$rootScope', 'StatService', function ($scope, $rootScope, statService) {
        $scope.onlyNode = false;
        $scope.visitCountMap = {};
        $scope.visitPercentMap = {};

        $scope.reloadChart = function(divId, title, stat, width) {
            var series = [];
            var serie = {
                type: 'pie',
                name: '访问量',
                data: []
            };
            var total_count = 0;
            for (var i = 0; i < stat.series.length; i++) {
                var visit_name = stat.series[i].name;
                var visit_count = stat.series[i].data[visit_name];
                total_count += visit_count;
            }
            for (var i = 0; i < stat.series.length; i++) {
                var visit_name = stat.series[i].name;
                var visit_count = stat.series[i].data[visit_name];

                var visit_percent = 0;
                if (visit_count > 0) {
                    visit_percent = (visit_count * 100 / total_count).toFixed(2) - 0;
                }
                serie.data.push([visit_name, visit_percent]);
                $scope.visitCountMap[visit_name] = visit_count;
                $scope.visitPercentMap[visit_name] = visit_percent;
            }
            series.push(serie);
            return new Highcharts.Chart({
                chart: {
                    plotBackgroundColor: null,
                    plotBorderWidth: null,
                    plotShadow: false,
                    renderTo: divId,
                    width: width
                },
                title: {
                    text: title,
                    x: -20
                },
                tooltip: {
                    formatter: function () {
                        return  '<b>' + this.point.name + '：</b>' + $scope.visitCountMap[this.point.name] + '(' + this.point.percentage.toFixed(2) + '%)';
                    }
                },
                plotOptions: {
                    pie: {
                        allowPointSelect: true,
                        cursor: 'pointer',
                        dataLabels: {
                            enabled: true,
                            format: '<b>{point.name}</b>: {point.percentage:.1f} %',
                            style: {
                                color: (Highcharts.theme && Highcharts.theme.contrastTextColor) || 'black'
                            }
                        }
                    }
                },
                series: series
            });
        };

        $scope.getStat = function (param) {
            $scope.param = param;
            statService.getVisitStat(param, function (stat) {
                //数据图表
                var chartWidth = Math.max($("#chart_panel").width(), $('#list_panel').width());
                if ($scope.chart != null) {
                    $scope.chart.destroy();
                }
                $scope.chart = $scope.reloadChart('chart', '访问量统计', stat, chartWidth);

                //数据列表
                var list = [];
                var totalVisit = 0;
                for (var i = 0; i < stat.series.length; i++) {
                    var serie = stat.series[i];
                    list.push({
                        name: serie.name,
                        count: serie.data[serie.name],
                        percent: $scope.visitPercentMap[serie.name]
                    });
                    totalVisit += serie.data[serie.name] - 0;
                }
                $scope.list = list;
                $scope.totalVisit = totalVisit;
            });
        };

        $scope.getHomeStatusStat = function() {
            $scope.param = {
                dimension: $rootScope.DIMENSION_COLLECTION,
                startTime: new Date(new Date().getTime() - 24 * 3600 * 1000).getDayBeginTime(),
                endTime: new Date(new Date().getTime() - 24 * 3600 * 1000).getDayEndTime()
            };
            statService.getStatusStat($scope.param, function (stat) {
                if ($scope.homeChart != null) {
                    $scope.homeChart.destroy();
                }
                $scope.homeChart = $scope.reloadChart('visitChart', '访问量统计', stat);
            });
        };
    }]);
});